<template>
	<view class="container">
		<view class="display-type margin-b-xs">
			<view class="flex-wrp flex-jun" style="height: 120rpx;">
				<view class="titet">标题</view>
				<view class="inputBox flex-wrp" style="width: auto;">
					<input class="input1" type="text" v-model="title" maxlength="14" placeholder="请输入名称" />
				</view>
			</view>
		</view>
		<view class="display-type margin-b-xs" v-if="categoryList&&categoryList.length">
			<view class="flex-wrp flex-jun" style="height: 120rpx;">
				<view class="titet">报告分类</view>
				<view style="min-width: 300rpx;text-align: right;">
					<picker @change="bindPickerChange" :value="categoryIndex" :range="categoryList" range-key="categoryName">
						<view class="uni-input">{{categoryList[categoryIndex].categoryName}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="display-type margin-b-xs">
			<view class="flex-wrp flex-jun" style="height: 120rpx;">
				<view class="titet">报告类型</view>
				<view style="min-width: 300rpx;text-align: right;">
					<picker @change="bindPickerChange1" :value="typeIndex" :range="typeList" range-key="title">
						<view class="uni-input">{{typeList[typeIndex].title}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="display-type margin-b-xs">
			<view class="flex-wrp flex-jun" style="height: 120rpx;">
				<view class="titet">收费金额</view>
				<view class="inputBox flex-wrp" style="width: auto;">
					<input class="input1" type="number" v-model="amount" maxlength="14" placeholder="请输入收费金额" />
				</view>
			</view>
		</view>
		<view class="display-type margin-b-xs" style="padding: 30rpx;height: auto;">
			<view class="titet" style="margin-bottom: 30rpx;">预览图片</view>
			<view class="imgBox">
				<view v-for="(item,value) in imageList" :key="value" class="picBox">
					<image :src="item" mode="aspectFill"></image>
					<image :src="imgHead+'keke/photo/20230223/1677127055194.png'" class="deletePic" @click="deletePic(value)"></image>
				</view>
				<image class="addPic" :src="imgHead+'keke/photo/20230223/1677127023225.png'" mode="aspectFill" v-if="imageList.length<9" @click="uploadPicFn"></image>
			</view>
		</view>
		<view class="display-type margin-b-xs" v-if="this.typeList[this.typeIndex].id==0">
			<view class="flex-wrp flex-jun" style="height: 120rpx;">
				<view class="titet">报告附件</view>
				<view class="uploadItem">
					<yt-upload ref="lsjUpload" childId="upload1" :width="fileWidth" :height="fileHeight" :option="fileOption" :size="fileSize"
						:formats="fileFormats" :debug="fileDebug" :instantly="fileInstantly" @change="fileOnChange" @uploadEnd="fileUpLoadEnd">
						<view class="">{{fileName?fileName:'添加文件'}}</view>
					</yt-upload>
				</view>
			</view>
		</view>
		<view class="display-type margin-b-xs" style="padding-top: 30rpx;height: auto;position: relative;margin-bottom: 200rpx;">
			<view class="titet" style="margin-bottom: 30rpx;">介绍</view>
			<textarea class="input1" v-model="detail" maxlength="1000" placeholder="请输入介绍" />
			<view class="lengthTxt">{{detail?detail.length:0}}/1000</view>
		</view>
		<view class="card-btn">
			<view class="add-card" @click="sureMessageFn">确定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgHead: 'https://qr-card.oss-accelerate.aliyuncs.com/',
				logo:'',
				title:'',
				detail:'',
				imageList:[],
				userInfo:{},
				categoryIndex:0,
				categoryList:[],
				typeIndex:0,
				amount:'',
				//0一次性报告 1日报 2周报 3月报
				typeList:[{id:0,title:'一次性报告'},{id:1,title:'日报'},{id:2,title:'周报'},{id:3,title:'月报'}],
				fileUrl:'',
				/* 文件上传 ---------------------- 开始 -------------------------------*/
				// APP 上传文件
				// 上传接口参数
				fileOption: {
					// 上传服务器地址，需要替换为你的接口地址
					url: '', // 该地址非真实路径，需替换为你项目自己的接口地址
					// 上传附件的key
					name: 'file',
				},
				// 选择文件后是否立即自动上传，true=选择后立即上传
				fileInstantly: true,
				// 必传宽高且宽高应与slot宽高保持一致
				fileWidth: '100%',
				fileHeight: '100%',
				// 限制允许上传的格式，空串=不限制，默认为空
				// fileFormats: 'txt,ppt,docx,doc,xlsx,xls,pptx',
				fileFormats: 'word,doc,docx,rtf,excel,xlsx,xls,ppt,pptx,pdf,txt',
				// fileFormats: '',
				// 文件上传大小限制
				fileSize: 150,
				// 是否打印日志
				fileDebug: true,
				/* 文件上传 ---------------------- 结束 -------------------------------*/
				fileName:'',
			}
		},
		onLoad(e) {
			this.userInfo = uni.getStorageSync('userInfo')
			this.initFn()
		},
		onShow() {
			this.fileOption.url = this.$baseUrl + '/common/upload'
		},
		methods: {
			initFn(){
				this.$http('/api/category/list').then(res=>{
					this.categoryList = res.result
				})
			},
			uploadPicFn(){
				uni.chooseImage({
					count: 9-this.imageList.length,
					success: (res) => {
						console.log(res.tempFilePaths)
						res.tempFilePaths.forEach(item=>{
							uni.uploadFile({
								url: this.$baseUrl + '/api/userUpload',
								filePath: item,
								header:{ Authorization : 'Bearer '+uni.getStorageSync('token')},
								name: 'file',
								success: (res1)=> {
									this.imageList.push(JSON.parse(res1.data).result)
								}
							});
						})
					}
				});
			},
			deletePic(val){
				this.imageList.splice(val,1)
			},
			sureMessageFn(){
				if(!this.title){
					uni.showToast({
						title:'请填写标题',
						icon:'none'
					})
					return
				}
				if(!this.detail){
					uni.showToast({
						title:'请填写报告简介',
						icon:'none'
					})
					return
				}
				if(this.typeList[this.typeIndex].id==0&&(!this.fileUrl)){
					uni.showToast({
						title:'请上传报告附件',
						icon:'none'
					})
					return
				}
				uni.showLoading({mask:true})
				let form = {
					title:this.title,
					content:this.detail,
					type: this.typeList[this.typeIndex].id,
					categoryId: this.categoryList[this.categoryIndex].id,
					imgUrl:this.imageList.join(),
					fileUrl:this.fileUrl,
				}
				if(!this.amount || this.amount<=0){
					this.$set(form,'isCharge',0)
					this.$set(form,'amount',0)
				}else {
					this.$set(form,'isCharge',1)
					this.$set(form,'amount',this.amount)
				}
				this.$http('/api/report/add',form,'POST').then(res=>{
					if(res.code == 200){
						uni.redirectTo({
							url:'/pages/space/list'
						}).then(res=>{
							uni.showToast({
								title:'发布成功，请等待管理员审核',
								icon:'none'
							})
						})
					}else {
						uni.showToast({
							title:res.message,
							icon:'none'
						})
					}
				})
			},
			bindPickerChange(e){
				this.categoryIndex = e.detail.value
			},
			bindPickerChange1(e){
				this.typeIndex = e.detail.value
			},
			bindDateChange(e){
				this.subscriptionDay = e.detail.value
			},
			/* APP 上传文件 ---------------------------------- 开始 --------------------------------------- */
			// 文件选择回调
			fileOnChange(files) {
				
				let arr = [...files.values()]
				console.log(arr)
				if(arr&&arr.length){
					uni.showLoading({
						mask:true
					})
				}else {
					uni.showToast({
						title:'请上传正确格式的文件',
						icon:'none'
					})
				}
				console.log('当前选择的文件列表：', JSON.stringify([...files.values()]));
				// this.option.formData.multipartFile = [...files.values()][0];
				// 强制更新视图
				this.$forceUpdate();
			},
			// 上传 完成之后的回调
			fileUpLoadEnd(item){
				let that = this;
				console.log(item,'上传之后的回调')
				console.log(JSON.parse(item.responseText),'上传之后的回调')
				this.fileUrl = JSON.parse(item.responseText).result
				uni.hideLoading()
				this.fileName = item.name
			},
			/* APP 上传文件 ---------------------------------- 结束 --------------------------------------- */
			
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #F0F1F6;
		min-height: 100vh;
		padding: 20rpx 24rpx;
	}
	image {
		width: 100%;
		height: 100%;
	}
	.display-type{
		padding: 0 30rpx;
		height: 116rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		.titet{
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 400;
			font-size: 32rpx;
			color: #333333;
			line-height: 32rpx;
			height: 32rpx;
		}
		.icon-box{
			height: 32rpx;
			.txt{
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				line-height: 28rpx;
			}
		}
		.arrowR{
			width: 10rpx;
			height: 20rpx;
			display: flex;
			image{
				width: 100%;
				height: 100%;
			}
		}
		.display-type-icon{
			width: 10rpx;
			height: 20rpx;
			margin-left: 20rpx;
		}
		.inputBox{
			width: 260rpx;
			height: 28rpx;
			font-weight: 400;
			font-size: 28rpx;
			color: #ABAAAF;
			line-height: 28rpx;
			text-align: right;
		}
		.iconBox{
			width: 108rpx;
			height: 108rpx;
			border-radius: 88rpx;
			image{
				width: 100%;
				height: 100%;
				border-radius: 88rpx;
			}
		}
		.input1 {
			color: #333333;
			font-size: 28rpx;
			width: 100%;
		}
		.color-list{
			.color-item{
				border: 2rpx solid transparent;
				border-radius: 14rpx 14rpx 14rpx 14rpx;
				padding: 6rpx;
				margin-left: 48rpx;
				view{
					width: 48rpx;
					height: 48rpx;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
				}
				.color1{
					background: #FF6060;
				}
				.color2{
					background: #337BE6;
				}
				.color3{
					background: #35CC2D;
				}
			}
			.active{
				border: 2rpx solid #005CFF;
			}
		}
	}
	.card-btn{
		width: 100%;
		background: #FFFFFF;
		padding: 20rpx 20rpx 60rpx;
		
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99;
		.add-card{
			height: 94rpx;
			background: #005CFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: bold;
			font-size: 32rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 94rpx;
			z-index: 99;
		}
	}
	.positioning{
		height: 128rpx;
		padding: 0 24rpx;
		.text{
			width: 532rpx;
			height: 72rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			.txt11{
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
				line-height: 28rpx;
				height: 28rpx;
			}
			.txt22{
				font-weight: 400;
				font-size: 24rpx;
				color: #777777;
				line-height: 24rpx;
				height: 24rpx;
			}
		}
	}
	
	.temporarilyImg {
		padding-top: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		image {
			width: 246rpx;
			height: 176rpx;
		}
	}
	
	.space-alliance{
		.add-alliance{
			margin: 20rpx 0;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 42rpx 0;
			
			display: flex;
			justify-content: center;
			align-items: center;
			gap: 20rpx;
			.add-alliance-icon{
				width: 32rpx;
				height: 32rpx;
				image{
					height: 100%;
					width: 100%;
				}
			}
			.add-alliance-txt{
				font-weight: 500;
				font-size: 32rpx;
				color: #3A87FF;
				line-height: 32rpx;
			}
		}
		
	}
	.space-alliance-list{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx 30rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		.space-alliance-list-info{
			display: flex;
			justify-content: center;
			align-items: center;
			gap: 20rpx;
			.space-alliance-list-icon{
				width: 108rpx;
				height: 108rpx;
				border-radius: 100%;
				image{
					width: 100%;
					height: 100%;
					border-radius: 100%;
				}
			}
			.space-alliance-list-main{
				max-width: 372rpx;
				height: 100rpx;
				
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				align-items: flex-start;
				gap: 20rpx;
				.space-alliance-list-main-title{
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
					height: 48rpx;
					line-height: 48rpx;
					
					max-width: 372rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 1;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.space-alliance-list-main-bottom{
					display: flex;
					justify-content: center;
					align-items: center;
					gap: 16rpx;
					.bottom-area-box{
						display: flex;
						justify-content: center;
						align-items: center;
						
						padding: 4rpx 10rpx;
						height: 32rpx;
						background: #F3F4F8;
						border-radius: 6rpx;
						
						font-weight: 400;
						font-size: 24rpx;
						color: #5C6375;
						line-height: 24rpx;
					}
					.bottom-city-box{
						display: flex;
						justify-content: center;
						align-items: center;
						
						padding: 4rpx 10rpx;
						height: 32rpx;
						background: #E3F3FF;
						border-radius: 6rpx;
						
						font-weight: 400;
						font-size: 24rpx;
						color: #4185FF;
						line-height: 24rpx;
					}
				}
			}
		}
		.space-alliance-list-handle{
			width: 112rpx;
			height: 60rpx;
			border-radius: 44rpx;
			border: 2rpx solid #E6E6E6;
			color: #333333;
			
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
	.margin-b-xs{
		margin-bottom: 20rpx;
	}
	.margin-r-xs {
		margin-right: 20rpx;
	}
	.lengthTxt {
		position: absolute;
		top: 26rpx;
		right: 30rpx;
		font-size: 28rpx;
		color: #AAAAAA;
	}
	
	.imgBox {
		width: 100%;
		height: 100%;
		display: flex;
		flex-wrap: wrap;
		.picBox {
			width: 200rpx;
			height: 200rpx;
			margin: 0 16rpx 16rpx 0;
			position: relative;
			&:nth-child(3n){
				margin: 0 0;
			}
			.deletePic {
				position: absolute;
				width: 40rpx;
				height: 40rpx;
				top: -10rpx;
				right: -10rpx;
			}
		}
		.addPic {
			width: 200rpx;
			height: 200rpx;
		}
	}
</style>
